package net.codejava.spring.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import net.codejava.spring.config.ResponseData;
import net.codejava.spring.dao.ContactDAO;
import net.codejava.spring.model.Contact;

/**
 * This controller routes accesses to the application to the appropriate
 * hanlder methods. 
 * @author www.codejava.net
 *
 */
@Controller
public class HomeController {

	@Autowired
	private ContactDAO contactDAO;
	
	@RequestMapping(value="/")
	public ModelAndView listContact(ModelAndView model) throws IOException{
		List<Contact> listContact = contactDAO.list();
		model.addObject("listContact", listContact);
		model.setViewName("home");
		
		return model;
	}
	
	@RequestMapping(value = "/newContact", method = RequestMethod.GET)
	public ModelAndView newContact(ModelAndView model) {
		Contact newContact = new Contact();
		model.addObject("contact", newContact);
		model.setViewName("ContactForm");
		return model;
	}
	
	@RequestMapping(value = "/saveContact", method = RequestMethod.POST)
	public ModelAndView saveContact(@ModelAttribute Contact contact) {
		contactDAO.saveOrUpdate(contact);		
		return new ModelAndView("redirect:/");
	}
	
	@RequestMapping(value = "/deleteContact", method = RequestMethod.GET)
	public ModelAndView deleteContact(HttpServletRequest request) {
		int contactId = Integer.parseInt(request.getParameter("id"));
		contactDAO.delete(contactId);
		return new ModelAndView("redirect:/");
	}
	
	@RequestMapping(value = "/editContact", method = RequestMethod.GET)
	public ModelAndView editContact(HttpServletRequest request) {
		int contactId = Integer.parseInt(request.getParameter("id"));
		Contact contact = contactDAO.get(contactId);
		ModelAndView model = new ModelAndView("ContactForm");
		model.addObject("contact", contact);
		
		return model;
	}
	
	/**
	 */
	@RequestMapping(value = "/getContactById.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData getContact(HttpServletRequest request) throws Exception {
		
		ResponseData response = null;
		try{
			int contactId = Integer.parseInt(request.getParameter("id"));
			Contact contact = contactDAO.get(contactId);
			response = new ResponseData(true, contact, 1, 1, null);
			response.setMessage("success");
			return response;
		}catch(Exception e){
			response = new ResponseData();
			response.setSuccess(false);
			response.setMessage("Error");
		}
		return response;
	}
	
	/**
	 */
	@RequestMapping(value = "/login.pjson",method = {RequestMethod.POST,RequestMethod.GET})
	public @ResponseBody ResponseData login(HttpServletRequest request) throws Exception {
		
		ResponseData response = null;
		try{
			String username = request.getParameter("username");
			String password = request.getParameter("password");
			Contact contact = contactDAO.get(username, password);
			if (contact!=null)  {
				response = new ResponseData(true, contact, 1, 1, null);
				response.setMessage("success");
			} else {
				response = new ResponseData();
				response.setSuccess(false);
				response.setMessage("error");
			}
		}catch(Exception e){
			response = new ResponseData();
			response.setSuccess(false);
			response.setMessage("error");
		}
		return response;
	}
}
